package com.spx.core.types;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;

import org.apache.commons.lang.StringUtils;

import com.spx.kernel.types.AbstractModelType;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Role extends AbstractModelType
{

	@ManyToMany
	private Set<Activity> activities;

	boolean isOn;

	public boolean isOn()
	{
		return isOn;
	}

	public void setOn(boolean on)
	{
		isOn = on;
	}

	public Set<Activity> getActions()
	{
		if (activities == null)
		{
			activities = new HashSet<Activity>();
		}

		return activities;
	}

	public void remove(Activity activity)
	{
		getActions().remove(activity);
	}

	public boolean canPerform(String actionName)
	{
		boolean result = false;
		for (final Activity activity : activities)
		{
			if (StringUtils.equalsIgnoreCase(actionName, activity.getName()))
			{
				result = true;
				break;
			}
		}
		return result;
	}

	public void add(Activity activity)
	{
		getActions().add(activity);
	}

	public void setAction(Set<Activity> newActions)
	{
		getActions().addAll(newActions);
		getActions().retainAll(newActions);
	}

}
